#!/bin/bash

precision=3 # digits after the '.'
grabber="$(which grabc 2>/dev/null)"
me="$(basename "$0")"
precision=3
outpath="$HOME/.config/xmountaincolors"

function usage {
  echo "  $1
  
  $me [theme name]

  interactively creates a theme for xmountaincolors by converting five 6-digit
  hex colors \"#nnnnnn\" (must use quotes), each color to 3 rgb values between
  0 and 1, and outputs them to a file that is understood by xmountaincolors.

  grabc is currently supported as a color grabber.

  theme name is to be given on the command line; the rest is interactive.
  the theme is saved to $outpath/[theme name].conf,
  because that is where xmountaincolors is looking for them.
"
exit 1
}
function get_and_convert {
	# gets a hex color and outputs a string in the form of
	# { red = n.nnn; green = n.nnn; blue = n.nnn; }
	until [[ "$col" =~ ^#[0-9A-Fa-f]{6}$ ]]
	do
		read -n 1 x
		case $x in
			'')
			[[ "x$grabber" != "x" ]] && echo "  grabbing... "; col="$($grabber 2>/dev/null)"
			;;
			'#')
			read col
			col="#$col"
			;;
			*)
			sleep 0.1
			echo
			;;
		esac
		[[ "$col" =~ ^#[0-9A-Fa-f]{6}$ ]] || echo -n "  Try again: "
	done
	red="$((16#${col:1:2}))"
	grn="$((16#${col:3:2}))"
	blu="$((16#${col:5:2}))"
	col=''
	echo -n "{ red = " >> "$file"
	printf "%${precision}.${precision}f " $(bc<<<"scale=$precision;$red/255") >> "$file"
	echo -n "; green = " >> "$file"
	printf "%${precision}.${precision}f " $(bc<<<"scale=$precision;$grn/255") >> "$file"
	echo -n "; blue = " >> "$file"
	printf "%${precision}.${precision}f" $(bc<<<"scale=$precision;$blu/255") >> "$file"

	#~ bc<<<"scale=$precision;x=$red/255;if(x>0 && x<1) print 0;print x"
	#~ echo -n "; green = "
	#~ bc<<<"scale=$precision;x=$grn/255;if(x>0 && x<1) print 0;print x"
	#~ echo -n "; blue = "
	#~ bc<<<"scale=$precision;x=$blu/255;if(x>0 && x<1) print 0;print x"

	echo -n "; }" >> "$file"
}

which bc >/dev/null 2>&1 || usage "command line calculator bc not found in \$PATH. Cannot proceed."

[[ $# != 1 ]] && usage "Please provide a theme name."

if [[ ! -d "$outpath" ]]; then
	read -n1 -p "
  Directory $outpath does not exist.
  Do you want to create it [y]? " x
	echo
	[[ "$x" == "y" ]] && mkdir -p "$outpath" || usage "You have chosen not to create $outpath.
  Cannot continue."
fi
file="$outpath/${1}.conf"

[[ -e "$file" ]] && usage "File $file already exists.
  Remove file or choose a different name."

touch "$file" || usage "Cannot open $file for writing."

read -p "
  Theme name and/or comment: " name

if [[ "x$name" != "x" ]]; then
	name="${name//\"/\\\"}"
	# i think doublequotes are the only characters libconfig chokes on, so escaping them here
fi

echo -n "name = \"$name\"
colors = {
base = ( " >> "$file"

echo "
  3 basecolors, from bottom (low altitudes) to top (high altitudes)
"

for altitude in low medium high; do
	echo -n "  Enter base color for $altitude altitudes"
	[[ "x$grabber" != "x" ]] && echo -n ", or <Enter> to grab with $(basename "$grabber")"
	echo -n ": "

	get_and_convert
	[[ "$altitude" != "high" ]] && echo -n ", " >> "$file" || echo " );" >> "$file"

done

for color in black white sky sea; do

	echo -n "$color = ( " >> "$file"

	echo -n "  Enter color for $color"
	[[ "x$grabber" != "x" ]] && echo -n ", or <Enter> to grab with $(basename "$grabber")"
	echo -n ": "

	get_and_convert
	echo " );" >> "$file"

done
echo " };" >> "$file"

read -n1 -p "  Do you want to see the config file $file now [y]? " x
echo
[[ "$x" == "y" ]] && cat "$file"
